package com.test.myfreemark;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class MyFreeMarker {
	private static Configuration configuration = new Configuration();
	private static Template template;
	public static String ftlDir="sqlserver";
	private static Writer writer;
	public static boolean CreateCode(String packgeprefix,Set<String> FileTypes,Set<String> TableNames,String FileSavePath,String detailPackge) throws Exception{
		System.out.println(ftlDir);
		configuration.setDirectoryForTemplateLoading(new File("ftls/"+ftlDir));
		for (String TABLENAME : TableNames) {
			Map<String, Object> root = new HashMap<String, Object>();
			List<Map<String,String>> TABLECOLUMS = DBUtils.getTableClums(TABLENAME);
			for (String FileType : FileTypes) {
				MyModel model = MyModel.getModel(FileType);
				model.setTableName(TABLENAME);
				model.setDetailPackge(detailPackge.replace(".", ""));//细包
				model.setBeanName(MyModel.getBeanName(TABLENAME));
				model.setVarName(MyModel.getPropertyName(TABLENAME));//变量名
				model.setProperty(MyModel.getPropertys(TABLECOLUMS));
				if(FileType.equals("MAPPER")){
					//mapper文件,设置命名空间
					model.setNameSpance(packgeprefix+model.getPackageName()+detailPackge+"."+model.getBeanName()+"Dao");
					model.setPackageName(packgeprefix+model.getPackageName()+detailPackge+".imp");//设置包名路径
				}else{
					model.setPackageName(packgeprefix+model.getPackageName()+detailPackge);//设置包名路径
				}
				if("BEAN".equals(FileType)){
					PrintWriter  pw = new PrintWriter(new FileWriter(FileSavePath+"/mybatis.xml", true)) ;
					pw.println("<typeAlias alias=\""+model.getBeanName()+"\" type=\""+model.getPackageName()+"."+model.getBeanName()+"\" />");
					pw.flush();
					pw.close();
				}else if("DAO".equals(FileType)){
					model.getImportBao().add("import "+packgeprefix+".model"+detailPackge+"."+model.getBeanName());
					model.getImportBao().add("import com.renbang.dao.BaseDao");
				}else if("SERVICE".equals(FileType)){
					model.getImportBao().add("import com.renbang.service.BaseServicesImp");
					model.getImportBao().add("import "+packgeprefix+".model"+detailPackge+"."+model.getBeanName());
					model.getImportBao().add("import "+packgeprefix+".dao"+detailPackge+"."+model.getBeanName()+"Dao");
				}else if("ACTION".equals(FileType)){ 
					model.getImportBao().add("import "+packgeprefix+".model"+detailPackge+"."+model.getBeanName());
					model.getImportBao().add("import "+packgeprefix+".service"+detailPackge+"."+model.getBeanName()+"Service");
					model.getImportBao().add("import com.renbang.action.BaseAction");
				}
				String dirPath = FileSavePath+"/"+model.getPackageName().replace(".", "/");
				File fileDir = new File(dirPath);
				if (!fileDir.exists()) {
					fileDir.mkdirs();
				}
				template = configuration.getTemplate(model.getTemplateName());
				String filePath = dirPath + "/"+model.getBeanName()+model.getPreFix()+model.getSufFix();
				File file = new File(filePath);
				if (!file.exists()) {
					file.createNewFile();
				}
				root.put("MODEL", model);
				writer = new FileWriter(file);
				template.process(root, writer);
				writer.flush();
			}
			
			 
		}
		writer.close();
		return false;
	}
}
enum FtlDir{
	ORACLE("oracle"),MYSQL("mysql"),SQLSERVER("sqlserver");
	private String dir;
	private FtlDir(){
		
	}
	private FtlDir(String dir){
		this.dir=dir;
	}
	public String getDir(){
		return dir;
	}
}
